package com.itheima.jobs;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.constant.RedisConstant;
import com.itheima.utils.QinniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

@Component
public class ClearImages {
    @Autowired
    JedisPool jedisPool;
    public void run(){
        System.out.println("清理图片"+new Date());
        Jedis jedis = jedisPool.getResource();
        Set<String> images = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);//数据多的放前面
//        if(CollectionUtil.isNotEmpty(images)){
//            for (String image : images) {
//                //删除七牛图片
//                QinniuUtil.delete(image);
//                //需要删除多余的数据
//                jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES,image);
//            }
//
//        }
        if(CollectionUtil.isNotEmpty(images)){
            String[] arr=new String[images.size()];
            images.toArray(arr);
            jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES,arr);
        }

        jedis.close();
    }
}
